﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class ArticleBLL
    {
        private static ArticleBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static ArticleBLL getInstance()
        {
            if (instance == null)
            {
                instance = new ArticleBLL();
            }
            return instance;
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddArticle(SC_Article obj)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                entites.AddToSC_Article(obj);
                return entites.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 根据条件查询文章
        /// </summary>
        /// <returns></returns>
        public List<SC_Article> GetArticleByCondition(SC_Article obj, int pageIndex, int pageSize, out int rowCount)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                rowCount = 0;
                var list = from r in entites.SC_Article.Where(a => a.IsDelete == false)
                           select r;
                if (obj.IsDelete != null)
                {
                    list = list.Where(a => a.IsDelete == obj.IsDelete);
                }
                if (obj.CategoryId > 0)
                {
                    list = list.Where(a => a.CategoryId == obj.CategoryId);
                }
                if (obj.ArticleTitle.Length > 0)
                {
                    list = list.Where(a => a.ArticleTitle.Contains(obj.ArticleTitle));
                }
                if (obj.KeyWord.Length > 0)
                {
                    list = list.Where(a => a.KeyWord.Contains(obj.KeyWord));
                }
                if (obj.Tag.Length > 0)
                {
                    list = list.Where(a => a.Tag.Contains(obj.Tag));
                }
                if (obj.PublishDate != null)
                {
                    list = list.Where(a => a.PublishDate == obj.PublishDate);
                }
                if (obj.IsHot != null)
                {
                    list = list.Where(a => a.IsHot == obj.IsHot);
                }
                if (obj.IsTag != null)
                {
                    list = list.Where(a => a.IsTag == obj.IsTag);
                }
                if (obj.IsChoice != null)
                {
                    list = list.Where(a => a.IsChoice == obj.IsChoice);
                }
                if (pageSize > 0)
                {
                    rowCount = list.Count<SC_Article>();
                    list = list.OrderByDescending(a => a.PublishDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                }

                return list.ToList<SC_Article>();
            }
        }

        /// <summary>
        /// 根据文章Id查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public SC_Article GetSC_ArticleById(int articleId)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                return entites.SC_Article.FirstOrDefault(r => r.IsDelete == false && r.Id == articleId);
            }
        }


        /// <summary>
        /// 提取5个标记的咨询
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<SC_Article> GetArticleTag()
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                return entites.SC_Article.OrderBy(a => a.Id).Where(a => a.IsTag == true && a.IsDelete == false).Take(5).ToList<SC_Article>();
            }
        }

        /// <summary>
        /// 提取5个精华的咨询
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<SC_Article> GetArticleChoice()
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                return entites.SC_Article.OrderBy(a => a.Id).Where(a => a.IsChoice == true && a.IsDelete == false).Take(5).ToList<SC_Article>();
            }
        }

        /// <summary>
        /// 提取5个热门的咨询
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public List<SC_Article> GetArticleHot()
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                return entites.SC_Article.OrderBy(a => a.Id).Where(a => a.IsHot == true && a.IsDelete == false).Take(5).ToList<SC_Article>();
            }
        }

        public bool Update(SC_Article obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_Article objSC_SC_Article = entities.SC_Article.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objSC_SC_Article);
                objSC_SC_Article.ArticleContent = obj.ArticleContent;
                objSC_SC_Article.ArticleTitle = obj.ArticleTitle;
                objSC_SC_Article.CategoryId = obj.CategoryId;
                objSC_SC_Article.CreateDate = obj.CreateDate;
                objSC_SC_Article.CreateUrl = obj.CreateUrl;
                objSC_SC_Article.IsChoice = obj.IsChoice;
                objSC_SC_Article.IsDelete = obj.IsDelete;
                objSC_SC_Article.IsHot = obj.IsHot;
                objSC_SC_Article.IsPass = obj.IsPass;
                objSC_SC_Article.IsPicCover = obj.IsPicCover;
                objSC_SC_Article.IsTag = obj.IsTag;
                objSC_SC_Article.KeyWord = obj.KeyWord;
                objSC_SC_Article.PicCoverUrl1 = obj.PicCoverUrl1;
                objSC_SC_Article.PicCoverUrl2 = obj.PicCoverUrl2;
                objSC_SC_Article.PicCoverUrl3 = obj.PicCoverUrl3;
                objSC_SC_Article.PicCoverUrl4 = obj.PicCoverUrl4;
                objSC_SC_Article.PicCoverUrl5 = obj.PicCoverUrl5;
                objSC_SC_Article.PublishDate = obj.PublishDate;
                objSC_SC_Article.PublishPeopleId = obj.PublishPeopleId;
                objSC_SC_Article.Tag = obj.Tag;
                entities.Attach(objSC_SC_Article);
                var newSC_Article = entities.ObjectStateManager.GetObjectStateEntry(objSC_SC_Article);
                newSC_Article.SetModified();
                newSC_Article.SetModifiedProperty("ArticleContent");
                newSC_Article.SetModifiedProperty("ArticleTitle");
                newSC_Article.SetModifiedProperty("CategoryId");
                newSC_Article.SetModifiedProperty("CreateDate");
                newSC_Article.SetModifiedProperty("CreateUrl");
                newSC_Article.SetModifiedProperty("IsChoice");
                newSC_Article.SetModifiedProperty("IsDelete");
                newSC_Article.SetModifiedProperty("IsHot");
                newSC_Article.SetModifiedProperty("IsPass");
                newSC_Article.SetModifiedProperty("IsPicCover");
                newSC_Article.SetModifiedProperty("IsTag");
                newSC_Article.SetModifiedProperty("PicCoverUrl1");
                newSC_Article.SetModifiedProperty("PicCoverUrl2");
                newSC_Article.SetModifiedProperty("PicCoverUrl3");
                newSC_Article.SetModifiedProperty("PicCoverUrl4");
                newSC_Article.SetModifiedProperty("PicCoverUrl5");
                newSC_Article.SetModifiedProperty("PublishDate");
                newSC_Article.SetModifiedProperty("PublishPeopleId");
                newSC_Article.SetModifiedProperty("Tag");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public List<SC_Article> GetArticleByCategoryId(int categoryId)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                return entites.SC_Article.OrderByDescending(a => a.PublishDate).Where(a => a.CategoryId == categoryId && a.IsDelete == false).ToList<SC_Article>();
            }
        }

        public bool Delete(int id)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                SC_Article obj = entites.SC_Article.FirstOrDefault(r => r.Id == id);
                obj.IsDelete = true;
                return entites.SaveChanges() > 0 ? true : false;
            }
        }

    }
}
